{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "528f1eb6-271f-48be-b0fb-5820ca21c5d6",
   "metadata": {},
   "source": [
    "# Standard Gates\n",
    "\n",
    "The Classiq platform provides many standard gates.\n",
    "Some key standard gates are shown here in detail. <br />\n",
    "All gates are covered in the [reference manual](https://docs.classiq.io/latest/reference-manual/python-sdk/#classiq.interface.generator.standard_gates)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "60fe7e57-d4e7-4c03-9104-ed3cec1177ab",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:48.571281Z",
     "iopub.status.busy": "2024-05-07T13:23:48.568658Z",
     "iopub.status.idle": "2024-05-07T13:23:51.251852Z",
     "shell.execute_reply": "2024-05-07T13:23:51.251041Z"
    }
   },
   "outputs": [],
   "source": [
    "# Generic imports\n",
    "from classiq import QArray, QBit, allocate, create_model, qfunc, synthesize, write_qmod"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3298ff3-c891-475b-acef-e581b5252f70",
   "metadata": {},
   "source": [
    "## Single Qubit Gates\n",
    "\n",
    "An example is given for $X$ gate. The gates $I$, $X$, $Y$, $Z$, $H$, $T$ are used in the same way."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e201bfba-7656-47a5-9f3d-5d479383b917",
   "metadata": {},
   "source": [
    "### For example: X\n",
    "Function: `X`\n",
    "\n",
    "Arguments:\n",
    "\n",
    "- `target`: `QBit`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "26931e36-bb1a-4003-9d8e-c6533bfacc9a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:51.256166Z",
     "iopub.status.busy": "2024-05-07T13:23:51.255667Z",
     "iopub.status.idle": "2024-05-07T13:23:52.780590Z",
     "shell.execute_reply": "2024-05-07T13:23:52.779953Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import X\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main():\n",
    "    q = QBit(\"q\")\n",
    "    allocate(1, q)\n",
    "\n",
    "    X(q)\n",
    "\n",
    "\n",
    "qmod = create_model(main)\n",
    "write_qmod(qmod, \"X\")\n",
    "qprog = synthesize(qmod)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b246605-e907-485f-8e03-3835180564ca",
   "metadata": {},
   "source": [
    "## Single Qubit Rotation Gates\n",
    "\n",
    "An example is given for $RZ$ gate. The gates $RX$, $RY$, $RZ$ are used in the same way except for parameter name.\n",
    "\n",
    "#### Parameter names for different rotation gates\n",
    "\n",
    "- `RX`: `theta`\n",
    "- `RY`: `theta`\n",
    "- `RZ`: `phi`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcad9460-e221-4d53-85e6-dac9f9397897",
   "metadata": {},
   "source": [
    "### For example: RZ\n",
    "\n",
    "$$\n",
    "\\begin{split}RZ(\\theta) = \\begin{pmatrix}\n",
    "{e^{-i\\frac{\\theta}{2}}} & 0 \\\\\n",
    "0 & {e^{i\\frac{\\theta}{2}}} \\\\\n",
    "\\end{pmatrix}\\end{split}\n",
    "$$\n",
    "\n",
    "Function: `RZ`\n",
    "\n",
    "Arguments:\n",
    "\n",
    "- `theta`: `CReal`\n",
    "- `target`: `QBit`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c93cd414-9cac-4221-90b1-0b7b5e375ca4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:52.783713Z",
     "iopub.status.busy": "2024-05-07T13:23:52.783183Z",
     "iopub.status.idle": "2024-05-07T13:23:53.886505Z",
     "shell.execute_reply": "2024-05-07T13:23:53.885814Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import RZ\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main():\n",
    "    q = QBit(\"q\")\n",
    "    allocate(1, q)\n",
    "\n",
    "    theta = 1.9\n",
    "    RZ(theta, q)\n",
    "\n",
    "\n",
    "qmod = create_model(main)\n",
    "write_qmod(qmod, \"RZ\")\n",
    "qprog = synthesize(qmod)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db881855-1856-44bb-a9b6-14d5c0a300d8",
   "metadata": {},
   "source": [
    "### R Gate\n",
    "\n",
    "Rotation by $\\theta$ around the $cos(\\phi)X + sin(\\phi)Y$ axis.\n",
    "\n",
    "$$\n",
    "\\begin{split}R(\\theta, \\phi) = \\begin{pmatrix}\n",
    "cos(\\frac{\\theta}{2}) & -ie^{-i\\phi}sin(\\frac{\\theta}{2}) \\\\\n",
    "-ie^{i\\phi}sin(\\frac{\\theta}{2}) & cos(\\frac{\\theta}{2}) \\\\\n",
    "\\end{pmatrix}\\end{split}\n",
    "$$\n",
    "\n",
    "Parameters:\n",
    "\n",
    "- `theta`: `CReal`\n",
    "- `phi`: `CReal`\n",
    "- `target`: `QBit`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e091727f-901d-4ff0-8e8b-420825aedfc5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:53.891400Z",
     "iopub.status.busy": "2024-05-07T13:23:53.889971Z",
     "iopub.status.idle": "2024-05-07T13:23:56.057376Z",
     "shell.execute_reply": "2024-05-07T13:23:56.056550Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import R\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main():\n",
    "    q = QBit(\"q\")\n",
    "    allocate(1, q)\n",
    "\n",
    "    theta = 1\n",
    "    phi = 2\n",
    "    R(theta, phi, q)\n",
    "\n",
    "\n",
    "qmod = create_model(main)\n",
    "write_qmod(qmod, \"R\")\n",
    "qprog = synthesize(qmod)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06c0c63b-ea3c-43b1-adcf-4a84117f5929",
   "metadata": {},
   "source": [
    "### Phase Gate\n",
    "\n",
    "Rotation about the Z axis by $\\lambda$ with global phase of $\\frac{\\lambda}{2}$.\n",
    "\n",
    "$$\n",
    "\\begin{split}X = \\begin{pmatrix} 1 & 0 \\\\ 0 & e^{i\\lambda} \\end{pmatrix}\\end{split}\n",
    "$$\n",
    "\n",
    "Parameters:\n",
    "\n",
    "- `theta`: `CReal`\n",
    "- `target`: `QBit`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6e7396cd-28a0-4dec-83ea-2a06817b2749",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:56.061885Z",
     "iopub.status.busy": "2024-05-07T13:23:56.060455Z",
     "iopub.status.idle": "2024-05-07T13:23:58.184795Z",
     "shell.execute_reply": "2024-05-07T13:23:58.184029Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import PHASE\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main():\n",
    "    q = QBit(\"q\")\n",
    "    allocate(1, q)\n",
    "\n",
    "    theta = 1\n",
    "    PHASE(theta, q)\n",
    "\n",
    "\n",
    "qmod = create_model(main)\n",
    "write_qmod(qmod, \"PHASE\")\n",
    "qprog = synthesize(qmod)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "355c2ca8-5c60-4d28-a825-e3fdca3ec60a",
   "metadata": {},
   "source": [
    "## Double Qubits Rotation Gates\n",
    "\n",
    "An example is given for $RZZ$ gate. The gates $RXX$, $RYY$, $RZZ$ are used in the same way.\n",
    "\n",
    "### RZZ Gate\n",
    "\n",
    "Rotation about ZZ.\n",
    "\n",
    "$$\n",
    "\\begin{split}RZZ(\\theta) = \\begin{pmatrix}\n",
    "{e^{-i\\frac{\\theta}{2}}} & 0 & 0 & 0 \\\\\n",
    "0 & {e^{i\\frac{\\theta}{2}}} & 0 & 0 \\\\\n",
    "0 & 0 & {e^{i\\frac{\\theta}{2}}} & 0 \\\\\n",
    "0 & 0 & 0 & {e^{-i\\frac{\\theta}{2}}} \\\\\n",
    "\\end{pmatrix}\\end{split}\n",
    "$$\n",
    "\n",
    "\n",
    "Parameters:\n",
    "\n",
    "- `theta`: `CReal`\n",
    "- `target`: `QArray[QBit]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8e1afacc-ea99-4bbe-8cdc-a9aee51d58ca",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:58.190034Z",
     "iopub.status.busy": "2024-05-07T13:23:58.188827Z",
     "iopub.status.idle": "2024-05-07T13:23:59.349718Z",
     "shell.execute_reply": "2024-05-07T13:23:59.349060Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import RZZ\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main():\n",
    "    q = QArray(\"q\")\n",
    "    allocate(2, q)\n",
    "\n",
    "    theta = 1\n",
    "    RZZ(theta, q)\n",
    "\n",
    "\n",
    "qmod = create_model(main)\n",
    "write_qmod(qmod, \"RZZ\")\n",
    "qprog = synthesize(qmod)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75bea4e9-7195-4a9c-a5bc-5fd43193220a",
   "metadata": {},
   "source": [
    "## Controlled Gates\n",
    "\n",
    "An example is given for $CX$ gate. The gates $CX$, $CY$, $CZ$, $CH$, $CSX$, $CCX$ are used in a similar way.\n",
    "\n",
    "In $CCX$ Gate the `ctrl_state` parameter receives a value suitable for 2 control qubits. for example: `\"01\"`.\n",
    "\n",
    "### CX Gate\n",
    "\n",
    "The Controlled $X$ gate.\n",
    "\n",
    "Applies $X$ Gate on the target qubit, based on the state of the control qubit\n",
    "(by default if the controlled state is $|1\\rangle$).\n",
    "\n",
    "$$\n",
    "\\begin{split}CX = \\begin{pmatrix}\n",
    "1 & 0 & 0 & 0 \\\\\n",
    "0 & 1 & 0 & 0 \\\\\n",
    "0 & 0 & 0 & 1 \\\\\n",
    "0 & 0 & 1 & 0 \\\\\n",
    "\\end{pmatrix}\\end{split}\n",
    "$$\n",
    "\n",
    "\n",
    "Parameters:\n",
    "\n",
    "- `control`: `QBit`\n",
    "- `target`: `QBit`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "db26e697-0c46-42ac-977e-4f1e5525486e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:59.352392Z",
     "iopub.status.busy": "2024-05-07T13:23:59.352004Z",
     "iopub.status.idle": "2024-05-07T13:24:00.479581Z",
     "shell.execute_reply": "2024-05-07T13:24:00.478797Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import CX\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main():\n",
    "    q_target = QBit(\"q_t\")\n",
    "    allocate(1, q_target)\n",
    "\n",
    "    q_control = QBit(\"q_c\")\n",
    "    allocate(1, q_control)\n",
    "\n",
    "    CX(q_control, q_target)\n",
    "\n",
    "\n",
    "qmod = create_model(main)\n",
    "write_qmod(qmod, \"CX\")\n",
    "qprog = synthesize(qmod)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6509986-4f44-4481-b88e-e05b6f19b048",
   "metadata": {},
   "source": [
    "## Controlled Rotations\n",
    "\n",
    "An example is given for $CRX$ gate. The gates $CRX$, $CRY$, $CRZ$, CPhase are used in the same way.\n",
    "\n",
    "### CRX Gate\n",
    "\n",
    "Controlled rotation around the X axis.\n",
    "\n",
    "$$\n",
    "\\begin{split}CRX(\\theta) = \\begin{pmatrix}\n",
    "1 & 0 & 0 & 0 \\\\\n",
    "0 & 1 & 0 & 0 \\\\\n",
    "0 & 0 & \\cos(\\frac{\\theta}{2}) & -i\\sin(\\frac{\\theta}{2}) \\\\\n",
    "0 & 0 & -i\\sin(\\frac{\\theta}{2}) & \\cos(\\frac{\\theta}{2}) \\\\\n",
    "\\end{pmatrix}\\end{split}\n",
    "$$\n",
    "\n",
    "Parameters:\n",
    "\n",
    "- `theta`: `CReal`\n",
    "- `control`: `QBit`\n",
    "- `target`: `QBit`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a362c33c-06d3-476e-9e4f-adbec2763977",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:24:00.484938Z",
     "iopub.status.busy": "2024-05-07T13:24:00.483684Z",
     "iopub.status.idle": "2024-05-07T13:24:02.659590Z",
     "shell.execute_reply": "2024-05-07T13:24:02.658976Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import CRX\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main():\n",
    "    q_target = QBit(\"q_t\")\n",
    "    allocate(1, q_target)\n",
    "\n",
    "    q_control = QBit(\"q_c\")\n",
    "    allocate(1, q_control)\n",
    "\n",
    "    theta = 1\n",
    "    CRX(theta, q_control, q_target)\n",
    "\n",
    "\n",
    "qmod = create_model(main)\n",
    "write_qmod(qmod, \"CRX\")\n",
    "qprog = synthesize(qmod)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bed9783a-1469-423e-8ed5-e51de881e036",
   "metadata": {},
   "source": [
    "## Swap Gate\n",
    "\n",
    "Swaps between two qubit states.\n",
    "\n",
    "$$\n",
    "\\begin{split}SWAP = \\begin{pmatrix}\n",
    "1 & 0 & 0 & 0 \\\\\n",
    "0 & 0 & 1 & 0 \\\\\n",
    "0 & 1 & 0 & 0 \\\\\n",
    "0 & 0 & 0 & 1 \\\\\n",
    "\\end{pmatrix}\\end{split}\n",
    "$$\n",
    "\n",
    "Parameters:\n",
    "\n",
    "- `qbit0`: `QBit`\n",
    "- `qbit1`: `QBit`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "10bf72bc-603d-4a63-9463-82fe91936e89",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:24:02.662793Z",
     "iopub.status.busy": "2024-05-07T13:24:02.662298Z",
     "iopub.status.idle": "2024-05-07T13:24:04.786181Z",
     "shell.execute_reply": "2024-05-07T13:24:04.785499Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import SWAP\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main():\n",
    "    q1 = QBit(\"q1\")\n",
    "    allocate(1, q1)\n",
    "\n",
    "    q2 = QBit(\"q2\")\n",
    "    allocate(1, q2)\n",
    "\n",
    "    SWAP(q1, q2)\n",
    "\n",
    "\n",
    "qmod = create_model(main)\n",
    "write_qmod(qmod, \"SWAP\")\n",
    "qprog = synthesize(qmod)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82d75454-20cb-4ad8-a025-600501867a84",
   "metadata": {},
   "source": [
    "## U Gate\n",
    "\n",
    "The single-qubit gate applies phase and rotation with three Euler angles.\n",
    "\n",
    "Matrix representation:\n",
    "\n",
    "$$\n",
    "U(\\gamma,\\phi,\\theta,\\lambda) = e^{i\\gamma}\\begin{pmatrix}\n",
    "\\cos(\\frac{\\theta}{2}) & -e^{i\\lambda}\\sin(\\frac{\\theta}{2}) \\\\\n",
    "e^{i\\phi}\\sin(\\frac{\\theta}{2}) & e^{i(\\phi+\\lambda)}\\cos(\\frac{\\theta}{2}) \\\\\n",
    "\\end{pmatrix}\n",
    "$$\n",
    "\n",
    "Parameters:\n",
    "\n",
    "- `theta`: `CReal`\n",
    "- `phi`: `CReal`\n",
    "- `lam`: `CReal`\n",
    "- `gam`: `CReal`\n",
    "- `target`: `QBit`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "654f9ae6-5bc0-43cd-bbc3-475ae8aa96ca",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:24:04.790744Z",
     "iopub.status.busy": "2024-05-07T13:24:04.789639Z",
     "iopub.status.idle": "2024-05-07T13:24:05.908123Z",
     "shell.execute_reply": "2024-05-07T13:24:05.907382Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import U\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main():\n",
    "    q = QBit(\"q\")\n",
    "    allocate(1, q)\n",
    "\n",
    "    theta = 1\n",
    "    phi = 2\n",
    "    lam = 1.5\n",
    "    gam = 1.1\n",
    "    U(theta, phi, lam, gam, q)\n",
    "\n",
    "\n",
    "qmod = create_model(main)\n",
    "write_qmod(qmod, \"U\")\n",
    "qprog = synthesize(qmod)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
